package com.test.ssm.controller;

import com.test.ssm.pojo.Student;
import com.test.ssm.pojo.User;
import com.test.ssm.service.LoginLogService;
import com.test.ssm.service.StudentService;
import com.test.ssm.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author 徒有琴
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    //我是新增的代码1
    //我是右边的，我也写了代码
    //左左左左
    //右右右右
    //2.0000000222222

    @Autowired
    private LoginLogService loginLogService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("/index.html")
    public String index(Model model) {
        //计算首页的日期
        List<String> date = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -6);
        date.add(format.format(calendar.getTime()));
        for (int i = 0; i < 6; i++) {
            calendar.add(Calendar.DATE, 1);
            date.add(format.format(calendar.getTime()));
        }
        model.addAttribute("dates", date);
        return "student/index";
    }

    @RequestMapping("/logs.html")
    @ResponseBody
    public List<String> logs(HttpSession session, String day) {
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        List<String> logs = loginLogService.getLogList(user.getId(), day);
        return logs;
    }

    @RequestMapping("/info.html")
    public String info(HttpSession session, Model model) {
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        Student student = studentService.getStudentById(user.getId());
        model.addAttribute("student", student);
        return "student/info";
    }

    @RequestMapping("/class.html")
    public String classInfo() {
        return "student/class";
    }
}
